/***************************************************************************
 *   Copyright (C) 2007 by Leonid Krashenko   *
 *   Leonid.Krashenko@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "frmedittask.h"
#include "todoitem.h"

#include <qtextedit.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qhbox.h>
#include <qmessagebox.h>

frmEditTask::frmEditTask(QWidget* parent, const char* name)
 : QDialog(parent, name, true)
{
	_item =0;
	
	init();
}

frmEditTask::frmEditTask(ToDoItem* item, QWidget* parent, const char* name)
	: QDialog(parent, name, true)
{
	_item = item;
	
	init();
}

frmEditTask::~frmEditTask()
{
}

void frmEditTask::init()
{
	QVBoxLayout* vbl = new QVBoxLayout(this); // text editor & hbl
	QHBoxLayout* hbl = new QHBoxLayout;	// buttons
	
	_txtedTask = new QTextEdit(this, "frmEditTask_txtedTask");
	QPushButton* btnOk, *btnSave, *btnCancel;
	
	btnOk = new QPushButton("OK", this, "frmEditTask_btnOk");
//	btnOk->setDefault(true);
	btnOk->setAccel(CTRL+Key_Return);
	btnSave = new QPushButton("&Save", this, "frmEditTask_btnSave");
	btnCancel = new QPushButton("&Cancel", this, "frmEditTask_btnCancel");
	
	hbl->addWidget(btnOk);
	hbl->addWidget(btnSave);
	hbl->addWidget(btnCancel);
	
	vbl->addWidget(_txtedTask);
	vbl->addLayout(hbl);
	
	connect (btnOk, SIGNAL(clicked()), this, SLOT(accept()) );
	connect (btnSave, SIGNAL(clicked()), this, SLOT(saveTaskChanges()) );
	connect (btnCancel, SIGNAL(clicked()), this, SLOT(close()) );
	connect (_txtedTask, SIGNAL(modificationChanged(bool)), this, SLOT(modify(bool)));
	
	if(_item){
		_txtedTask->setText(_item->getContent());
		setCaption(_item->getName());
	}
}

void frmEditTask::saveTaskChanges()
{	
	if(!_item)
		return;
	
	_item->setContent(_txtedTask->text());
	modify(false);
}

void frmEditTask::modify(bool modified)
{
	if(modified){
		setCaption(QString("*")+_item->getName());
	}
	else 
		setCaption(_item->getName());
}

void frmEditTask::accept()
{
	if(_item)
	{
		_item->setContent(_txtedTask->text());
	}
		
	QDialog::accept();
}
